package com.reebake.ideal.document.util;

import cn.hutool.extra.spring.SpringUtil;
import lombok.SneakyThrows;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;

import java.io.InputStream;
import java.io.OutputStream;

public class DocumentConvertUtil {

    private static DocumentConverter getDocumentConverter(){
        return SpringUtil.getBean(DocumentConverter.class);
    }

    @SneakyThrows
    public static void convert(InputStream is, String fromExt, OutputStream os, String toExt){
        getDocumentConverter().convert(is)
                .as(DefaultDocumentFormatRegistry.getFormatByExtension(fromExt))
                .to(os)
                .as(DefaultDocumentFormatRegistry.getFormatByExtension(toExt))
                .execute();
        os.flush();
    }
}
